<?php
/**
 *  订阅邮箱
*/

namespace app\api\service;

use app\facade\Basic;
use app\api\model\Subscribe as SubscribeModel;
use think\facade\Cache;

class Subscribe
{

    /**
     *  邮箱订阅
    */
    public function email($param)
    {

        if ( Cache::get('subscribe_'.md5($param['ip'])) ) {
            return Basic::data(0,'subscribe.duplicateSubscription');
        }

        $result = SubscribeModel::where(['email'=>$param['email']])->field('id')->find();
        if ( $result ) {
            return Basic::data(0,'subscribe.emailAlreadyExists');
        }

        $data = [];
        $data['email'] = $param['email'];
        $data['source'] = 1;
        $data['create_time'] = date('Y-m-d H:i:s',time());
        $data['status'] = 1;
        if (SubscribeModel::create($data)) {
            Cache::set('subscribe_'.md5($param['ip']),$param['email'],3600);
            return Basic::data(0,'subscribe.success');
        } else {
            return Basic::data(1,'subscribe.error');
        }
    }
}
